/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

public class TrayControl implements ActionListener, MouseListener {

	private static TrayControl instance;

	private Vector<PrionaceWindow> windowList;

	private boolean windowVisible;

	private TrayIcon trayIcon;

	private TrayMenu menu;

	private TrayControl() {
		windowList = new Vector<PrionaceWindow>();
		windowVisible = true;
	}

	public static TrayControl getInstance() {
		if (instance == null) {
			instance = new TrayControl();
		}
		return instance;
	}

	public void init() {
		if (SystemTray.isSupported()) {
			initTrayIcon();
		} else {
			initWindowOnly();
		}
	}

	private void initTrayIcon() {
		String resource;

		if (System.getProperty("os.name").toLowerCase().contains("win")) {
			resource = "/icons/prionace_logo_16x16.png";
		} else {
			resource = "/icons/prionace_logo_22x22.png";
		}

		SystemTray systemTray = SystemTray.getSystemTray();
		Image icon = Toolkit.getDefaultToolkit().getImage(
				getClass().getResource(resource));
		trayIcon = new TrayIcon(icon);
		trayIcon.addMouseListener(this);
		createMenu();

		try {
			systemTray.add(trayIcon);
			addWindow(System.getProperty("user.home"));
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	private void initWindowOnly() {
		addWindow(System.getProperty("user.home"));
	}

	public void addWindow(String path) {
		PrionaceWindow window = null;

		try {
			Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
			window = new PrionaceWindow(path);
			window.setSize(800, 600);
			window.setLocation((screen.width - window.getWidth()) / 2,
					(screen.height - window.getHeight()) / 2);
			window.setVisible(true);
			windowList.add(window);
		} catch (Exception e) {
			ExceptionHandler.showException(e, null);
		}
	}

	public boolean isWindowVisible() {
		return windowVisible;
	}

	public void setWindowVisible(boolean windowVisible) {
		PrionaceWindow window;
		int windowCount = 0;

		while (windowCount < windowList.size()) {
			window = windowList.elementAt(windowCount);
			if (!window.isDisplayable()) {
				windowList.remove(window);
			} else {
				windowCount++;
			}
		}

		if (windowList.size() == 0) {
			addWindow(System.getProperty("user.home"));
			this.windowVisible = true;
			return;
		}

		for (windowCount = 0; windowCount < windowList.size(); windowCount++) {
			window = windowList.elementAt(windowCount);
			window.setVisible(windowVisible);
		}

		this.windowVisible = windowVisible;
	}

	public TrayMenu getMenu() {
		return menu;
	}

	private void createMenu() {
		menu = new TrayMenu();
		TrayMenuItem newWindow = new TrayMenuItem("Nova janela",
				System.getProperty("user.home"));
		newWindow.addActionListener(this);
		menu.add(newWindow);
	}

	private void menuAt(Point location) {
		menu.setInvoker(menu);
		menu.setVisible(true);
		location.x -= menu.getSize().width;
		menu.setLocation(location);
	}

	public void actionPerformed(ActionEvent e) {
		TrayMenuItem selectedItem = (TrayMenuItem) e.getSource();
		addWindow(selectedItem.getPath());
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger()) {
			menuAt(e.getPoint());
		}
	}

	public void mouseReleased(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			setWindowVisible(!windowVisible);
		}

		if (e.isPopupTrigger()) {
			menuAt(e.getPoint());
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}
}
